package com.zrs.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zrs.gmall.common.cache.GmallCache;
import com.zrs.gmall.product.mapper.SpuSaleAttrMapper;
import com.zrs.gmall.product.model.*;
import com.zrs.gmall.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author ZhaoRuiSheng
 * @create 2023/2/22 20:01
 */
@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuPosterService spuPosterService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    /**
     * 分页查询商品SPU列表
     * @param infoPage
     * @param category3Id
     * @return
     */
    @Override
    public IPage<SpuInfo> getSpuByPage(IPage<SpuInfo> infoPage, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (category3Id != null) {
            queryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        }
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        return spuInfoService.page(infoPage, queryWrapper);
    }

    /**
     * 保存商品SPU信息
     * @param spuInfo
     * @return
     *
     * 1.将商品基本新存入spu_info表,保存成功后得到商品SPUID
     * 2.获取前端提交商品图片 将图片列表存入spu_image表
     * 3.获取前端提交海报图片 将图片列表存入spu_poster表
     * 4.获取前端提交销售属性列表 将信息存入spu_sale_attr表
     * 5.获取前端提交销售属性值列表 将信息存入spu_sale_attr_val表
     */
    @Override
    //多张表，考虑脏读
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品基本新存入spu_info表,保存成功后得到商品SpuId
        spuInfoService.save(spuInfo);
        //2.获取前端提交商品图片 将图片列表存入spu_image表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach(spuImage -> {
                //2.1将商品图片关联到商品SPU
                spuImage.setSpuId(spuInfo.getId());
            });
            //2.2批量保存商品图片
            spuImageService.saveBatch(spuImageList);
        }
        //3.获取前端提交海报图片 将图片列表存入spu_poster表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuInfo.getId());
            });
            spuPosterService.saveBatch(spuPosterList);
        }
        //4.获取前端提交销售属性列表 将信息存入spu_sale_attr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                //4.1 关联到商品SPU
                spuSaleAttr.setSpuId(spuInfo.getId());
                //5.获取前端提交销售属性值列表 将信息存入spu_sale_attr_val表
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        //5.1 关联商品SPU
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        //5.2 手动为销售属性名称赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    //5.3 批量保存销售属性值
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            //4.2 保存销售属性列表
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 根据SPUID查询商品销售属性列表(包含属性值)
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        //调用自定义SQL查询销售属性名称以及值
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return spuSaleAttrList;
    }

    /**
     * 根据商品SpuID查询当前商品所有图片
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuImage::getSpuId, spuId);
        return spuImageService.list(queryWrapper);
    }

    /**
     * 4、根据SPUID查询详情页海报图片列表
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "spuPosterBySpuId:", suffix = ":info")
    @Override
    public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId, spuId);
        return spuPosterService.list(queryWrapper);
    }

    /**
     * 6、根据SkuID，SpuID，查询当前商品所有的销售属性,判断为当前SKU拥有销售属性增加选中效果
     * @param skuId
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "spuSaleAttrListCheckBySku", suffix = ":info")
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        return spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId, spuId);
    }


}
